<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // eventbus.$on('event1', () => { })
    // eventbus.$emit('event1', options)
    // document.removeEventListener('event1')
    // 只有一个class，第三方
    // events {'click': [],'input': ,}
    class EventBus {
      events = {}
      on(eventName, callback) {
        if (!this.events[eventName]) {
          this.events[eventName] = []
        }
        this.events[eventName].push(callback)
        this.events[eventName] = [...new Set(this.events[eventName])]
      }
      emit(eventName, params) {
        (this.events[eventName] || []).forEach(item => {
          item(params)
        })
      }
      off(eventName, callback) {
        if (!callback) {
          delete this.events[eventName]
        } else {
          this.events[eventName] = this.events[eventName]?.filter(item => item !== callback)
        }
      }
    }
    const handle = (params) => {
      console.log('click', params)
    }
    const eventbus = new EventBus()
    eventbus.off('click', () => { })
    // eventbus.on('click', handle)
    // eventbus.on('click', handle)
    // eventbus.emit('click', { a: 1 })
    // const handle = () => {
    //   console.log('click')
    // }
    // document.addEventListener('click', () => {
    //   console.log('click')
    // })
    // document.addEventListener('click', () => {
    //   console.log('click')
    // })
  </script>
</body>

</html>